
package com.benimetum.beans;

import com.benimetum.items.Course;
import com.benimetum.items.Person;
import com.benimetum.items.Student;
import com.benimetum.managers.AnnouncementManager;
import com.benimetum.managers.CourseManager;
import com.benimetum.managers.LoginManager;
import com.benimetum.managers.StudentManager;
import com.benimetum.program.CourseProgram;
import java.io.IOException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Ethem Cem Ozkan
 * @email ethemcem.ozkan@gmail.com
 * No 07110604
 */
@ManagedBean(name="StudentBean")
@SessionScoped
public class StudentBean
{
    private Student target;
    private Course[] courses;
    private String text;
    private Date date;

    public void setText(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Date getDate() {
        return date;
    }

    public Course[] getCourses() {
        return courses;
    }
    
    public Student getTarget() {
      return target;
    }

    public void setCourses(Course[] courses) {
        this.courses = courses;
    }

    public void setTarget(Student target) {
        this.target = target;
        if(target.getStatus()==null){
            target.setStatus("Durum güncellemesi yok.");
        }
          courses=CourseManager.getStudentsCourse(target.getId());
    }
    
    public boolean same(){
        boolean equals = target.getId().equals(LoginManager.getUser().getId());
        return target.getId().equals(LoginManager.getUser().getId());
    }
    
    public Course[] courses(){
      return CourseManager.getStudentsCourse(target.getId());
    }
    
    public void changeStatus(){
      StudentManager.updateStudentsStatus(target.getId(), target.getStatus());
    }
    
    public String getProgram(){
      return CourseProgram.getCourseProgram(target.getId().replace("st", ""));
    }
    
    public String studentFinalProgram(){
      return CourseProgram.getFinalProgram(LoginManager.getUser().getId().replace("st", ""));
    }
    
    public void addIndependentAnnouncement(){
          Person[] array=new Person[1];
          array[0]=target;
	  AnnouncementManager.createAnnouncement(text, array, LoginManager.getUser().getId());
	  FacesContext context = FacesContext.getCurrentInstance();
	  HomePageBean obj = context.getApplication().evaluateExpressionGet(context, "#{HomePageBean}", HomePageBean.class);
	  obj.makeBoxFalse();
	  try {
		FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
	  } catch (IOException ex) {
		Logger.getLogger(CourseBean.class.getName()).log(Level.SEVERE, null, ex);
	  }
    }
    public void addIndependentEvent(){
          Person[] array=new Person[1];
          array[0]=target;
	  AnnouncementManager.createEvent(text, date,array, LoginManager.getUser().getId());
	  FacesContext context = FacesContext.getCurrentInstance();
	  HomePageBean obj = context.getApplication().evaluateExpressionGet(context, "#{HomePageBean}", HomePageBean.class);
	  obj.makeBoxFalse();
	  try {
		FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
	  } catch (IOException ex) {
		Logger.getLogger(CourseBean.class.getName()).log(Level.SEVERE, null, ex);
	  }
    }
}
